package com.example.wechatappbackend.controller.front;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.wechatappbackend.VO.OrderDishVO;
import com.example.wechatappbackend.VO.OrderVO;
import com.example.wechatappbackend.common.CommonResponse;
import com.example.wechatappbackend.common.JsonMsg;
import com.example.wechatappbackend.entity.Order;
import com.example.wechatappbackend.entity.OrderDish;
import com.example.wechatappbackend.service.OrderService;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order/")
public class OrderController {

    @Autowired
    private OrderService orderService;

//    通过openId来获取用户的订单列表
    @GetMapping("{openId}")
    @ResponseBody
    public JsonMsg getOrdersByUsername(@PathVariable("openId") String openId) {
        return orderService.getOrdersByOpenId(openId);
    }

    @GetMapping("/Dish/{orderId}")
    @ResponseBody
    public JsonMsg getOrderByOrderId(@PathVariable("orderId") String openId) {
        return orderService.getOrderByOrderId(openId);
    }

    //生成订单
    @GetMapping(value = "/add/{openId}",produces = "application/json;charset=UTF-8")
    @ResponseBody

    public CommonResponse insertOrder(    @PathVariable("openId") String openId,
                                          @RequestParam String orderTime,@RequestParam String peopleNum,
                                          @RequestParam String restaurantName,@RequestParam String restaurantAvatar,
                                          @RequestParam String orderPrice,@RequestParam String status) {

        OrderVO orderVO = new OrderVO();
        orderVO.setOrderTime(orderTime);
        orderVO.setPeopleNum(Integer.parseInt(peopleNum));
        orderVO.setRestaurantName(restaurantName);
        orderVO.setRestaurantAvatar(restaurantAvatar);
        orderVO.setOrderPrice(orderPrice);
        orderVO.setStatus(Integer.parseInt(status));
        return orderService.insertOrder(openId,orderVO);
    }

    //取消订单
    @PostMapping("/cancle/{orderId}")
    @ResponseBody
    public CommonResponse cancleOrder(@PathVariable("orderId") int orderId){
        return orderService.cancleOrder(orderId);
    }
}
